/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShAccountDoorDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShAccountDoorService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShAccountDoor;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShAccountDoorService 实现类
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shAccountDoorService")
public class ShAccountDoorServiceImpl extends BaseServiceImpl<ShAccountDoor, Long> implements ShAccountDoorService {

    @Resource
    private ShAccountDoorDao shAccountDoorsDao;

    public BaseDao<ShAccountDoor, Long> getDao() {
        return shAccountDoorsDao;
    }

	@Override
	public DataGrid queryDataGrid(ShAccountDoor accountDoor, PageHelper page) {
		DataGrid dataGrid = new DataGrid();
		ShAccountDoor queryAccountDoor = new ShAccountDoor();
        if (!StringUtils.isBlank(page.getSort())) {
        	queryAccountDoor.setOrderField(page.getSort());
        }
        if (!StringUtils.isBlank(page.getOrder())) {
        	queryAccountDoor.setOrderFieldType(page.getOrder());
        }
        if (null != accountDoor.getAccountId()) {//根据用户ID
        	queryAccountDoor.setAccountId(accountDoor.getAccountId());
        }
        if (null != accountDoor.getOwner()) {//是否业主
        	queryAccountDoor.setOwner(accountDoor.getOwner());;
        }
        if (null != accountDoor.getStatus()){
        	queryAccountDoor.setStatus(accountDoor.getStatus());
        }
        if(null != accountDoor.getDoorId()){//根据门牌ID查询
        	queryAccountDoor.setDoorId(accountDoor.getDoorId());
        }
        if(null != accountDoor.getUnitId()){//根据单元ID查询
        	queryAccountDoor.setUnitId(accountDoor.getUnitId());
        }
        if(null != accountDoor.getBuildingId()){//根据楼栋ID查询
        	queryAccountDoor.setBuildingId(accountDoor.getBuildingId());
        }
        if(null != accountDoor.getResidenceId()){//根据小区ID查询
        	queryAccountDoor.setResidenceId(accountDoor.getResidenceId());
        }
        if (null != accountDoor.getGmtCreate()) {
        	queryAccountDoor.addQueryData("gmtCreate", accountDoor.getGmtCreate());
        }
        
        
        long totalCount = shAccountDoorsDao.selectShAccountDoorCountByCon(queryAccountDoor);
        dataGrid.setTotal(totalCount);
        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        if (totalCount > 0 && totalCount > startIndex) {
        	queryAccountDoor.setStartIndex(startIndex);
        	queryAccountDoor.setEndIndex(endIndex);
            List<ShAccountDoor> deviceTypes = shAccountDoorsDao.selectShAccountDoorByCon(queryAccountDoor);
            dataGrid.setRows(deviceTypes);
        }
        return dataGrid;
	}

	@Override
	public List<ShAccountDoor> queryShAccountDoorByCon(ShAccountDoor accountDoor) {
		return shAccountDoorsDao.selectShAccountDoorByCon(accountDoor);
	}

	@Override
	public List<ShAccountDoor> getDetailList(Long accountId) {
		return shAccountDoorsDao.getDetailList(accountId);
	}
}